package com.koudai.reader.conponment;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.koudai.reader.android.R;
import com.koudai.reader.util.Constant;

public class SelectTypeDialog extends Dialog {
    
    GridView typelist;
    TypeListAdapter listAdapter;
    private Context context;
    
    private Map<Integer, Boolean> selectMapping = new HashMap<Integer, Boolean>();
    private int selectedCount = 0;
    private Button confirm;    
    
    public SelectTypeDialog(final Context context, final Handler handler) {
        super(context);
        this.context = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);           
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.sort_type_list);
        typelist = (GridView)findViewById(R.id.grid);
        
        listAdapter = new TypeListAdapter();
        typelist.setAdapter(listAdapter);
        
        SharedPreferences config = context.getSharedPreferences("config", 0);
        String [] selectSortType = config.getString("select_sort_type", "0").split(",");
        for (int i = 0; i < 8; i++) {
            selectMapping.put(i, false);
        }
        
        if (selectSortType.length == 3) {
            for (int i = 0; i < 3; i++) {

                selectMapping.put(Integer.valueOf(selectSortType[i].trim()).intValue() - 1, true);
            }
            
            selectedCount = 3;
        }

        
        setCancelable(false);
        
        confirm = (Button) findViewById(R.id.confirm_select);
        
        confirm.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                if (selectedCount == 3) {
                    
                    for (Entry<Integer, Boolean> item : selectMapping.entrySet()) {
                        if (item.getValue()) {
                            sb.append(item.getKey().intValue() + 1).append(",");
                        }
                    }
                    SharedPreferences config = context.getSharedPreferences("config", 0);
                    config.edit().putString("select_sort_type", sb.substring(0, sb.length() - 1)).commit();
                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);
                    dismiss();
                } else {
                    Toast.makeText(context, "请选择3个类型", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    class TypeListAdapter extends BaseAdapter {
        LayoutInflater inflater;
        
        public TypeListAdapter(){
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 8;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = inflater.inflate(R.layout.sort_type_item, null);
            final Button button = (Button)convertView.findViewById(R.id.image_item);
            button.setText(Constant.mainCategoryMapping.get(position + 1));
            
            if (selectMapping.get(position)) {
                button.setBackgroundResource(R.drawable.ic_button_unselected);
            } else {
                button.setBackgroundResource(R.drawable.bg_blue_btn_short);
            }
            
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (!selectMapping.get(position)) {
                        // 选上
                        if (selectedCount <= 2) {
                            selectMapping.put(position, true);
                            selectedCount++;
                            button.setBackgroundResource(R.drawable.ic_button_unselected);
                        } else {
                            Toast.makeText(context, "只能选择三个哦", Toast.LENGTH_LONG).show();
                        }

                    } else {
                        // 取消选择
                        if (selectedCount > 0) {
                            selectMapping.put(position, false);
                            selectedCount--;
                            button.setBackgroundResource(R.drawable.bg_blue_btn_short);
                        }
                    }
                    
                }

            });
            
            return convertView;
        }
        
    }
}
